package com.suarte.webapp.servlet;

import com.suarte.core.Product;
import com.suarte.core.service.ProductManager;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author Smahmud
 */
public class ProductValidate extends HttpServlet {

    private ProductManager productManager;
    private ApplicationContext appContext;

    static class Response {

        boolean success;

        public Response() {
            success = true;
        }

        public Response(boolean success, String message) {
            this.success = success;
        }
    }

    public void setProductManager(ProductManager productManager) {
        this.productManager = productManager;
    }
    

    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void activate(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (appContext == null) {
            appContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            productManager = (ProductManager) appContext.getBean("productManager");
        }
        response.setContentType("text/html");
        response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1
        response.setHeader("Pragma", "no-cache"); //HTTP 1.0
        response.setDateHeader("Expires", 0); //prevents caching at the proxy server

        Long id = Long.valueOf(request.getParameter("id"));
        Product product = productManager.get(id);

        Response resp = null;

        if (product == null) {
            resp = new Response(false, "Registro no encontrado");
            response.getWriter().append("{");
            response.getWriter().append("\"success\":" + (product != null ? "\"OK\"" : "\"FAIL\""));
            response.getWriter().append("}");
            response.getWriter().flush();
            response.getWriter().close();
        } else {
            try {
                resp = new Response();

                response.getWriter().append("{");
                response.getWriter().append("\"success\":" + (product != null ? "\"OK\"" : "\"FAIL\"") + ",");
                response.getWriter().append("\"item\":{" + "\"price\":" + (product.getPrice()) + "," + "\"description\":" + "\"" + product.getDescription() + "\"" + ","
                        + "\"unitMeasure\":" + "\"" +  product.getUnitMeasure().getDescription()  + "\"" + "," + "\"measure\":" + product.getMeasure() + "}");

                response.getWriter().append("}");
                response.getWriter().flush();
                response.getWriter().close();

            } catch (Exception e) {
                resp = new Response(false, e.getMessage());
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        activate(request, response);
    }

    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        activate(request, response);
    }

    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
